from rest_framework import serializers
from goods.models import SKU,GoodsCategory,SPU,SPUSpecification,SpecificationOption,SKUSpecification
from django.db import transaction

#1,商品sku的规格信息,序列化器
class SKUSpecificationSerializer(serializers.Serializer):
    spec_id = serializers.IntegerField()
    option_id = serializers.IntegerField()

#2,获取sku信息的序列化器
class SKUSerializer(serializers.ModelSerializer):

    #1,重写spu,spu_id
    spu = serializers.StringRelatedField(read_only=True)
    spu_id = serializers.IntegerField()

    #2,重写category,category_id
    category = serializers.StringRelatedField(read_only=True)
    category_id = serializers.IntegerField()

    #3,specs
    specs = SKUSpecificationSerializer(read_only=True,many=True)

    class Meta:
        model = SKU
        fields = "__all__"

    #4,重写create方法,保存sku对象,并设置sku对象的规格选项
    @transaction.atomic
    def create(self, validated_data):

        #0,设置保存点
        sid = transaction.savepoint()

        try:
            #1,创建sku对象
            sku = SKU.objects.create(**validated_data)

            #2,创建规格对象,关联sku
            specs = self.context["request"].data["specs"]
            for spec_dict in specs:
                SKUSpecification.objects.create(sku=sku,spec_id=spec_dict["spec_id"],option_id=spec_dict["option_id"])
        except Exception:
            transaction.savepoint_rollback(sid)
            raise serializers.ValidationError("创建sku,或者规格失败")
        else:
            #3,提交,返回响应
            transaction.savepoint_commit(sid)
            return sku

    #5,重写update方法,更新sku的规格选项
    @transaction.atomic
    def update(self, instance, validated_data):
        #0,设置保存点
        sid = transaction.savepoint()

        try:
            #1,更新sku其他数据(价格,副标题...)
            SKU.objects.filter(id=instance.id).update(**validated_data)

            #2,创建规格对象,关联sku
            specs = self.context["request"].data["specs"]
            for spec_dict in specs:
                SKUSpecification.objects.filter(sku_id=instance.id,spec_id=spec_dict["spec_id"]).update(option_id=spec_dict["option_id"])

        except Exception:
            transaction.savepoint_rollback(sid)
            raise serializers.ValidationError("创建sku,或者规格失败")
        else:
            #3,提交,返回响应
            transaction.savepoint_commit(sid)
            return SKU.objects.get(id=instance.id)


#3,获取sku的三级分类,序列化器
class SKUCategorySerializer(serializers.ModelSerializer):
    class Meta:
        model = GoodsCategory
        fields = ("id","name")

#4,获取spu信息,序列化器
class GoodSimpleSerializer(serializers.ModelSerializer):
    class Meta:
        model = SPU
        fields = ("id","name")

#5,获取spu规格信息
class SpecificationOptionSerializer(serializers.ModelSerializer):
    class Meta:
        model = SpecificationOption
        fields = ("id","value")

class SPUSpecsSerializer(serializers.ModelSerializer):

    #1,spu,spu_id
    spu = serializers.StringRelatedField(read_only=True)
    spu_id = serializers.IntegerField()

    #2,options选项
    options = SpecificationOptionSerializer(read_only=True,many=True)
    class Meta:
        model = SPUSpecification
        # fields = "__all__"
        exclude = ("create_time","update_time")